package com.example.baselibrary.http;

import com.example.baselibrary.common.BaseConstant;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Retrofit工厂，单例  by zqq on 2018/6/11.
 */

public class RetrofitFactory {

    private Retrofit retrofit;
    private Interceptor interceptor;    //通用拦截

    private static class Holder {
        private static final RetrofitFactory FACTORY = new RetrofitFactory();
    }

    public static RetrofitFactory getInstance() {
        return Holder.FACTORY;
    }

    //构造方法--创建客户端
    private RetrofitFactory() {
        //拦截器添加header等
        interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("Content_Type", "application/json")
                        .addHeader("charset", "UTF-8")
//                        .header("Authorization", "Basic dGVzdDp0ZXN0")
//                        .addHeader("token", (String) SPUtil.get(BaseApplication.context, SPUtil.SP_FILE, null))
                        .build();

                return chain.proceed(request);
            }
        };

        //Retrofit实例化
        retrofit = new Retrofit.Builder()
                .baseUrl(BaseConstant.SERVER_ADDRESS)
                .addConverterFactory(GsonConverterFactory.create())     //添加一个Gson转换器工厂。
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())//配置回调库，采用RxJava，增加返回值为Oservable<T>的支持
                .client(initClient())   //添加一个OKHttp--Client
                .build();
    }

    //OkHttp
    private OkHttpClient initClient() {
        //指定可以运行的线程的最大数目
        Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
        dispatcher.setMaxRequests(20);              //最大的请求数量
//        dispatcher.setMaxRequestsPerHost(1);        //主机同一个时间，最大的请求数量

        OkHttpClient builder = new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .addInterceptor(initLogInterceptor())       //添加自己的拦截器
                .addInterceptor(interceptor)
                .connectTimeout(10, TimeUnit.SECONDS)  //连接时间
                .readTimeout(10, TimeUnit.SECONDS)    //超时时间
                .build();
        return builder;
    }

    //构建自己的日志拦截器
    private Interceptor initLogInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return interceptor;
    }


    /**
     * 具体服务实例化
     */
    public <T> T create(Class<T> service) {
        return retrofit.create(service);
    }

}
